<?php
/**
 * Model for a project.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Model for a project.
 *
 * @package PhpCilux
 */
class Model_Project extends Model_Base
{

    /**
     * The project key of this project.
     *
     * @var string
     */
    protected $_projectKey;

    /**
     * The source dir.
     *
     * @var string
     */
    protected $_srcDir;

    /**
     * Set the project key.
     *
     * @param string $text The project key.
     *
     * @return Model_Project
     */
    public function setProjectKey($text)
    {
        $this->_projectKey = (string) $text;
        return $this;
    }

    /**
     * Get the project key.
     *
     * @return string
     */
    public function getProjectKey()
    {
        return $this->_projectKey;
    }

    /**
     * Get the current build status.
     *
     * @return string
     * @throws Exception Uncatched exceptions.
     */
    public function getBuildStatus()
    {
        try {
            $status = $this->getLastBuild()->getStatus();
        } catch (Exception $e) {

            if ('Can not find build' != $e->getMessage()) {
                throw $e;
            }

            $status = 'nobuild';
        }

        return $status;
    }

    /**
     * Get the name of the project.
     *
     * @return string
     */
    public function getName()
    {
        return $this->getProjectKey();
    }

    /**
     * Get the build mapper.
     *
     * @return Model_BuildMapper
     */
    protected function _getBuildMapper()
    {
        return Model_BuildMapper::getInstance();
    }

    /**
     * Get the last build.
     *
     * @return Model_Build
     * @throws Exception No build found.
     */
    public function getLastBuild()
    {
        $build = $this->_getBuildMapper()->getLast($this->_projectKey);

        if ($build instanceof Model_Build) {
            return $build;
        }

        throw new Exception('Can not find build');
    }

    /**
     * Get a build by the given build key.
     *
     * @param string $buildKey A build key.
     *
     * @return Model_Build
     * @throws Exception No build found.
     */
    public function getBuild($buildKey)
    {
        $build = $this->_getBuildMapper()->getBuild($this->_projectKey, $buildKey);

        if ($build instanceof Model_Build) {
            return $build;
        }

        throw new Exception('Can not find build');
    }

    /**
     * Get the previous build based on the given build key.
     *
     * @param string  $buildKey A build key.
     * @param integer $limit    Max count of builds.
     *
     * @return array Array of Model_Build
     */
    public function getPrevBuilds($buildKey, $limit = 10)
    {
        return $this->_getBuildMapper()->getPrevBuilds($this->_projectKey, $buildKey, $limit);
    }

    /**
     * Set the source dir.
     *
     * @param string $text The source dir.
     *
     * @return Model_Project
     */
    public function setSrcDir($text)
    {
        $this->_srcDir = (string) $text;
        return $this;
    }

    /**
     * Get the source dir.
     *
     * @return string
     */
    public function getSrcDir()
    {
        return $this->_srcDir;
    }

}